package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.common.core.constant.enums.SpecialCrimeTypeEnum;
import com.engine4cloud.tdf.common.core.util.lambda.Lambdas;
import com.engine4cloud.tdf.supervision.dto.create.JzdxJudgmentCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.DictSelectResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxJudgmentResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxSpecialCrimeResultDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxSpecialCrimeEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Mapper
public interface JzdxSpecialCrimeConvert {
    JzdxSpecialCrimeConvert INSTANCE = Mappers.getMapper(JzdxSpecialCrimeConvert.class);

    JzdxSpecialCrimeResultDTO convert(JzdxSpecialCrimeEntity s);

    default void convertList(JzdxJudgmentResultDTO result, List<JzdxSpecialCrimeResultDTO> specialCrimeList,
                             List<DictSelectResultDTO> fiveIndependenceItems, List<DictSelectResultDTO> fiveInvolvementItems, List<DictSelectResultDTO> fourHistoryItems) {
        if (CollectionUtils.isNotEmpty(specialCrimeList)) {
            Map<SpecialCrimeTypeEnum, List<JzdxSpecialCrimeResultDTO>> groupMap = specialCrimeList.stream()
                    .collect(Collectors.groupingBy(JzdxSpecialCrimeResultDTO::getTszxlx));
            //设置五独
            result.setFiveIndependenceList(setTypeName(groupMap.get(SpecialCrimeTypeEnum.FIVE_INDEPENDENCE), fiveIndependenceItems));
            //设置五涉
            result.setFiveInvolvementList(setTypeName(groupMap.get(SpecialCrimeTypeEnum.FIVE_INVOLVEMENT), fiveInvolvementItems));
            //设置四史
            result.setFourHistoryList(setTypeName(groupMap.get(SpecialCrimeTypeEnum.FOUR_HISTORY), fourHistoryItems));
        }
    }

    default List<JzdxSpecialCrimeResultDTO> setTypeName(List<JzdxSpecialCrimeResultDTO> specCrimeList, List<DictSelectResultDTO> items) {
        List<JzdxSpecialCrimeResultDTO> specList = new ArrayList<>();

        if (CollectionUtils.isEmpty(specCrimeList)) {
            return Collections.emptyList();
        }

        Map<String, DictSelectResultDTO> dictMap = items.stream().collect(Collectors.toMap(DictSelectResultDTO::getCode, v -> v));
        specCrimeList.forEach(crime -> {
            if (dictMap.containsKey(crime.getJtlx())) {
                crime.setJtlxmc(dictMap.get(crime.getJtlx()).getName());
                specList.add(crime);
            }
        });

        return specList;
    }

    default JzdxSpecialCrimeEntity convert(String code, SpecialCrimeTypeEnum category, String crime, Long tenantId) {
        JzdxSpecialCrimeEntity crimeEntity = new JzdxSpecialCrimeEntity();
        crimeEntity.setRybh(code);
        crimeEntity.setTszxlx(category);
        crimeEntity.setJtlx(crime);
        crimeEntity.setTenantId(tenantId);
        return crimeEntity;
    }

    default List<JzdxSpecialCrimeEntity> create(JzdxJudgmentCreateDTO s, Long tenantId) {
        List<JzdxSpecialCrimeEntity> specialCrimeEntityList = new ArrayList<>();
        Lambdas.each(s.getFiveIndependenceList(), v -> specialCrimeEntityList.add(convert(s.getRybh(), SpecialCrimeTypeEnum.FIVE_INDEPENDENCE, v, tenantId)));
        Lambdas.each(s.getFiveInvolvementList(), v -> specialCrimeEntityList.add(convert(s.getRybh(), SpecialCrimeTypeEnum.FIVE_INVOLVEMENT, v, tenantId)));
        Lambdas.each(s.getFourHistoryList(), v -> specialCrimeEntityList.add(convert(s.getRybh(), SpecialCrimeTypeEnum.FOUR_HISTORY, v, tenantId)));
        return specialCrimeEntityList;
    }

}

